<?php
/**
 * 素材维护任务
 * @author ice<iceinto@mallog.com.cn>
 */
class MaterialAction extends BaseAction
{

	public static $Grade = array();

	public function _initialize()
	{
		// 1展示底图 2小图底图 3添加品牌  4商场海报 5楼层中图 6商品特卖 7优惠领取
		self::$Grade['1'] = array(1,);
		self::$Grade['2'] = array(1, 2, 3, 4);
		self::$Grade['3'] = array(1, 2, 3, 4, 5, 6, 7);
		parent::_initialize();
		$this->assign('nav', 2);
		$this->assign('web_title', '维护任务');
	}

	/**
	 * 查看当前等级
	 * @return array
	 * @author ice<iceinto@mallog.com.cn>
	 */
	public function _checkGrade($uid)
	{
		//获取用户等级   role 3 level
		$level = D('User')->getMLevel($uid);
		$level = intval($level['level']);
		if ($level) {
			return self::$Grade[$level];
		} else {
			return false;
		}
	}

	/**
	 * 维护任务首页
	 * @author ice<iceinto@mallog.com.cn>
	 */
	public function index()
	{
		$mModel = M('Maintenance');
		$allCount = $mModel->count();
		$okCount = $mModel->where('status = 100')->count();
		$waitAllotCount = $mModel->where('status = 1')->count();
		$cancelCount = $mModel->where('status = 99')->count();
		for ($i = 1; $i <= 7; $i++) {
			$map['status'] = 1;
			$map['type'] = $i;
			$this->assign('wait' . $i, intval($mModel->where($map)->count()));
		}
		//获取美工列表
		$users = D('User')->getDesList();
		$this->assign('users', $users);
		$this->assign('waitCount', $allCount - $okCount - $cancelCount);
		$this->assign('waitAllotCount', $waitAllotCount);
		$this->assign('cancelCount', $cancelCount);
		$this->assign('allCount', $allCount);
		$this->display();
	}

	/**
	 * 对当前人员认为任务
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function statistics()
	{
		$uid = intval($this->_get('uid'));
		if(empty($uid)){
			$this->error('必须传递用户标示！');
		}
		//时间计算
		$Date = new Date();
		$year = intval(getGet('year'));
		$month = intval(getGet('month'));
		$grade = intval(getGet('grade'));
		if ($year && $month) {
			$Date = new Date($year . '-' . $month . '-1');
			$this->assign('month', $month);
		} else {
			$this->assign('month', date('n'));
		}
		$fireTime = $Date->firstDayOfMonth()->format("%Y-%m-%d");
		$lastTime = $Date->lastDayOfMonth()->format("%Y-%m-%d");

		$fireTime = strtotime($fireTime);
		$lastTime = strtotime($lastTime);
		//获取登陆用户基本信息
		$grades = $this->_checkGrade($uid);
		//根据条件获取任务列表
		$grade = empty($grade) ? 1 : $grade;
		$map = array();
		$map['m_maintenance_allot.uid'] = $uid;
		$map['m_maintenance.type'] = $grade;
		//写入结束时间
		$map['m_maintenance.end_time'] = array(array('egt', $fireTime), array('elt', $lastTime));
		$MaintenanceAllot = D('MaintenanceAllot');
		$taskCount = $MaintenanceAllot->getTaskCount($map);
		$taskImageCount = $MaintenanceAllot->getTaskImageCount($map);
		$Page = $this->getPage($taskCount);
		$tasks = $MaintenanceAllot->getTasks($map, $Page->firstRow . ',' . $Page->listRows);
		$this->assign('tasks', $tasks);
		$this->assign('page', $Page->show());
		//你月日设置
		$this->assign('years', $this->getYears($year));
		$this->assign('months', $this->getMonths($month));
		$this->assign('grade', $grade);
		$this->assign('grades', $grades);
		$this->assign('taskCount', $taskCount);
		$this->assign('taskImageCount', $taskImageCount);
		$this->display();
	}

	/**
	 * 获取任务过期的
	 */
	public function getExpire(){
		$year = intval(getGet('year'));
		$month = intval(getGet('month'));
		$Date = new Date();
		if ($year && $month) {
			$Date = new Date($year . '-' . $month . '-1');
			$this->assign('month', $month);
		} else {
			$this->assign('month', date('n'));
		}
		$fireTime = $Date->firstDayOfMonth()->format("%Y-%m-%d");
		$lastTime = $Date->lastDayOfMonth()->format("%Y-%m-%d");

		$fireTime = strtotime($fireTime);
		$lastTime = strtotime($lastTime);

		$map = array();
		$map['m_maintenance_allot.end_time'] = array(array('egt', $fireTime), array('elt', $lastTime));
		$ms = M('Maintenance')->field('m_maintenance.itemid,m_maintenance.brand,m_maintenance.type,m_maintenance.add_time,m_maintenance.end_time,m_maintenance.now_count,m_maintenance.end_count,
				m_maintenance_allot.uid,m_maintenance_allot.add_time as allot_start_time,m_maintenance_allot.end_time as allot_end_time,
				(m_maintenance_allot.end_time - m_maintenance.end_time) as exp_time')
		->join('INNER JOIN m_maintenance_allot ON m_maintenance_allot.mid = m_maintenance.itemid')
		->where($map)
		->having('exp_time > 86400')
		->select();
		$this->assign('ms', $ms);
		$this->assign('years', $this->getYears($year));
		$this->assign('months', $this->getMonths($month));
		$this->display();
	}
}